home *** CD-ROM | disk | FTP | other *** search
/ C++ für Kids / C++ for kids.iso / Buch / Roller1.cpp < prev    next >
C/C++ Source or Header  |  1999-01-25  |  5KB  |  173 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl\vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "Roller1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma resource "*.dfm"
  8.  
  9. class TChassis
  10. {
  11. protected:
  12.   int x1, x2, y1, y2;
  13.   int Breite, Hoehe, Diff;
  14. public:
  15.   TChassis (int X, int Y, int B);
  16.   virtual ~TChassis (void);
  17.   virtual void Show (void);
  18.   // hier steht die rein virtuelle Methode
  19.   virtual void Move (int Ziel) = 0;
  20. };
  21.  
  22. class TVelo : public TChassis
  23. {
  24. public:
  25.   TVelo (int X, int Y, int B);
  26.   virtual void Show (void);
  27.   // hier wird die rein virtuelle Methode ⁿberschrieben
  28.   virtual void Move (int Ziel) {};
  29. };
  30.  
  31. class TAuto : public TChassis
  32. {
  33. public:
  34.   TAuto (int X, int Y, int B);
  35.   virtual void Show (void);
  36.   // hier wird die rein virtuelle Methode ⁿberschrieben
  37.   virtual void Move (int Ziel) {};
  38. };
  39.  
  40. TAuto *Auto;
  41. TVelo *Velo;
  42. TForm1 *Form1;
  43.  
  44. //---------------------------------------------------------------------------
  45. TChassis::TChassis (int X, int Y, int B)
  46. {
  47.   x1 = X;
  48.   y1 = Y;
  49.   Breite = B;
  50.   Hoehe = Breite/2;
  51.   x2 = x1 + Breite;
  52.   y2 = y1 + Hoehe;
  53.   Diff = Breite/5;
  54. }
  55. //---------------------------------------------------------------------------
  56. TChassis::~TChassis (void)
  57. {
  58. }
  59. //---------------------------------------------------------------------------
  60. TVelo::TVelo (int X, int Y, int B)
  61.   : TChassis (X, Y, B)
  62. {
  63. }
  64. //---------------------------------------------------------------------------
  65. TAuto::TAuto (int X, int Y, int B)
  66.   : TChassis  (X, Y, B)
  67. {
  68. }
  69. //---------------------------------------------------------------------------
  70. void TChassis::Show (void)
  71. {
  72.   int Dicke = Breite/3;
  73.   int Felge = Dicke/6;
  74.  
  75.   // nur die beiden RΣder zeichnen
  76.   Form1->Canvas->Pen->Width = 2;
  77.   Form1->Canvas->Brush->Color = clInactiveCaption;
  78.   Form1->Canvas->Ellipse (x1,y2-Dicke,x1+Dicke,y2);
  79.   Form1->Canvas->Ellipse (x2-Dicke,y2-Dicke,x2,y2);
  80.   Form1->Canvas->Brush->Color = clActiveCaption;
  81.   Form1->Canvas->Ellipse
  82.     (x1+Felge,y2-Dicke+Felge,x1+Dicke-Felge,y2-Felge);
  83.   Form1->Canvas->Ellipse
  84.     (x2-Dicke+Felge,y2-Dicke+Felge,x2-Felge,y2-Felge);
  85. }
  86. //---------------------------------------------------------------------------
  87. void TVelo::Show (void)
  88. {
  89.   int Radius = Breite/6;
  90.   Form1->Refresh ();
  91.  
  92.   // Fahrgestell zeichnen
  93.   TChassis::Show ();
  94.  
  95.   // Rahmen zeichnen
  96.   Form1->Canvas->Pen->Width = 4;
  97.   Form1->Canvas->MoveTo (x1+Radius,y2-Radius);
  98.   Form1->Canvas->LineTo (x1+2*Radius,y1+Diff/3);
  99.   Form1->Canvas->LineTo (x1+3*Radius,y2-Radius);
  100.   Form1->Canvas->LineTo (x2-2*Radius,y1+Diff/3);
  101.   Form1->Canvas->LineTo (x2-Radius,y2-Radius);
  102.   Form1->Canvas->MoveTo (x1+2*Radius,y1+Diff/3);
  103.   Form1->Canvas->LineTo (x2-2*Radius,y1+Diff/3);
  104.  
  105.   // Lenker und Tretkurbel zeichnen
  106.   Form1->Canvas->Arc
  107.     (x2-2*Radius-Diff/2,y1,x2-2*Radius+Diff/2,y1+Diff/3,
  108.      x2-2*Radius,y1+Diff/3,x2-2*Radius,y1);
  109.   Form1->Canvas->MoveTo (x1+3*Radius,y1+Radius+2*Diff/5);
  110.   Form1->Canvas->LineTo (x1+3*Radius,y1+Radius+6*Diff/5);
  111.  
  112.   // Sattel und Pedale zeichnen
  113.   Form1->Canvas->Pen->Width = 1;
  114.   Form1->Canvas->Brush->Color = clActiveCaption;
  115.   Form1->Canvas->Ellipse
  116.     (x1+2*Radius-Diff/2,y1,x1+2*Radius+Diff/2,y1+Diff/3);
  117.   Form1->Canvas->Rectangle
  118.     (x1+3*Radius-Diff/5,y1+Radius+Diff/5,
  119.      x2-3*Radius+Diff/5,y1+Radius+2*Diff/5);
  120.   Form1->Canvas->Rectangle
  121.     (x1+3*Radius-Diff/5,y1+Radius+6*Diff/5,
  122.      x2-3*Radius+Diff/5,y1+Radius+7*Diff/5);
  123. }
  124. //---------------------------------------------------------------------------
  125. void TAuto::Show (void)
  126. {
  127.   int Radius = Breite/6;
  128.   Form1->Refresh ();
  129.  
  130.   // Karosserie zeichnen
  131.   Form1->Canvas->Pen->Width = 1;
  132.   Form1->Canvas->Brush->Color = clActiveCaption;
  133.   Form1->Canvas->RoundRect
  134.     (x1,y1+Breite/6-5,x2,y2-Diff/3, 30,30);
  135.  
  136.   // Fahrgestell zeichnen
  137.   TChassis::Show ();
  138.  
  139.   Form1->Canvas->Pen->Width = 4;
  140.   Form1->Canvas->MoveTo (x1+Diff/4,y1+Breite/6);
  141.   Form1->Canvas->LineTo (x2-Diff/4,y1+Breite/6);
  142.  
  143.   // Tⁿr und Fenster zeichnen
  144.   Form1->Canvas->Pen->Width = 1;
  145.   Form1->Canvas->RoundRect
  146.     (x1+2*Radius+5,y1,x2-2*Radius-5,y2-Diff/3,20,20);
  147.   Form1->Canvas->Pen->Width = 2;
  148.   Form1->Canvas->Brush->Color = clInactiveCaption;
  149.   Form1->Canvas->RoundRect
  150.     (x1+2*Radius+10,y1+10,x2-2*Radius-10,y1+Breite/4,20,20);
  151. }
  152. //---------------------------------------------------------------------------
  153. __fastcall TForm1::TForm1(TComponent* Owner)
  154.     : TForm(Owner)
  155. {
  156. }
  157. //---------------------------------------------------------------------------
  158. void __fastcall TForm1::FormCreate(TObject *Sender)
  159. {
  160.   Auto= new TAuto (60,20,310);
  161.   Velo= new TVelo (60,20,310);
  162. }
  163. //---------------------------------------------------------------------------
  164. void __fastcall TForm1::Button1Click(TObject *Sender)
  165. {
  166.   Velo->Show ();
  167. }
  168. //---------------------------------------------------------------------------
  169. void __fastcall TForm1::Button2Click(TObject *Sender)
  170. {
  171.   Auto->Show ();
  172. }
  173. //---------------------------------------------------------------------------